package com.jinyuanchun.learnjava.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jinyuanchun.learnjava.dao.PmsProject;
import com.jinyuanchun.learnjava.dao.User;
import com.jinyuanchun.learnjava.service.PmsProjectService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
public class Test {
    @Autowired
    private PmsProjectService pmsProjectService;


    @PostMapping(value = "/project",produces = "application/json;charset=UTF-8")
    public int test(@RequestBody List<PmsProject> pmsProjects) {
//        System.out.println("==========="+data);
//        List<PmsProject> pmsProjects = jsonStrToJava1(data);
        int d = pmsProjectService.insertOrUpdateByList(pmsProjects);
        return d;
    }

    private static List<PmsProject> jsonStrToJava1(String str){
        JSONObject jsonObject = JSONObject.fromObject(str);
        ArrayList<PmsProject> result = (ArrayList<PmsProject>)JSONObject.toBean(jsonObject, List.class);
        return result;
    }
    private static User jsonStrToJava2(String str){
        JSONObject jsonObject = JSONObject.fromObject(str);
        User user = (User)JSONObject.toBean(jsonObject, User.class);
        return user;
    }

//    private static <T> jsonStrToJava(String str){
//        JSONObject jsonObject = JSONObject.fromObject(str);
//        T t = (T)JSONObject.toBean(jsonObject, T.class);
//        return t;
//    }
}
